<?php
class PaymentCardProductController extends CpController
{
    public function actionIndex()
    {
        $rate_card = ARate::getAllRateCard();
        
        /* Lấy tất cả loại thẻ trong dữ liệu */
        $providerCode = ACardProduct::getAllProviderCode();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 30;
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["provider_code"] = isset($_GET["provider_code"]) ? trim(strip_tags($_GET["provider_code"])) : "";
        $search["type"] = isset($_GET["type"]) ? trim(strip_tags($_GET["type"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["order"] = isset($_GET["order"]) ? trim(strip_tags($_GET["order"])) : "";        
        
        $card = array();
        $totalProduct = 0;        
        $numberPage = 0;
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            
            /* Lấy danh sách sản phẩm phát sinh doanh thu trong khoảng thời gian tìm kiếm */
            $products = ACardProduct::GetProduct($search, $currentPage, $numberRecordPerPage);
            $totalProduct = ACardProduct::GetTotalProduct($search);
            $numberPage = ceil($totalProduct / $numberRecordPerPage);
            
            if(!empty($products)){
                $order = array();
                $search["list_game_id"] = "";                
                foreach($products as $i=>$row){
                    $search["list_game_id"] .= $row["type"]==1 ? ", " . $row["dataId"] : "";                    
                    
                    $order[$row["dataId"] . "_" . $row["type"]] = $i;
                }        
                $search["list_game_id"] = ltrim($search["list_game_id"], ", ");                

                /* Lấy log sms của các sản phẩm này */
                $card = ACardProduct::getReport($search, $rate_card, $order);                
            }   
        }
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "providerCode"=>$providerCode
                , "card"=>$card
                , "totalProduct"=>$totalProduct
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage                
            )
        );
    }
    
    public function actionDetail()
    {
        $rate_card = ARate::getAllRateCard();
        
        /* Lấy tất cả loại thẻ trong dữ liệu */
        $providerCode = ACardProduct::getAllProviderCode();        
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 50;
        
        $search = array();
        $search["dataId"] = isset($_GET["dataId"]) ? intval($_GET["dataId"]) : 0;
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "";
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : "";        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["provider_code"] = isset($_GET["provider_code"]) ? trim(strip_tags($_GET["provider_code"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["type"] = isset($_GET["type"]) ? trim(strip_tags($_GET["type"])) : "";
        
        $card = array();
        $totalCard = 0;        
        $numberPage = 0;
        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            
            /* Lấy log card của các thành viên này */
            $card = ACardProduct::getReportByProduct($search, $rate_card, $currentPage, $numberRecordPerPage);            
            $totalCard = ACardProduct::getTotalReportByProduct($search, $rate_card);            
            $numberPage = ceil($totalCard / $numberRecordPerPage);
        }
        
        $this->render(
            "detail"
            , array(
                "search"=>$search
                , "providerCode"=>$providerCode
                , "card"=>$card
                , "totalCard"=>$totalCard
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );   
    }
}  
?>